from html.parser import HTMLParser
import sys
import re
import pokeparser

class moveinfo(HTMLParser):
	p = pokeparser.pokeparser()

	def __init__(self, url):
		super().__init__()
		self.in_battle = self.in_list = self.in_range = self.in_ranges = self.in_effect = self.in_p = self.in_learn = self.in_table = self.in_style = self.in_inner \
			= self.in_span = self.in_tr = self.in_td = self.in_other = self.in_form = self.ofall = self.in_font = self.in_name = self.in_th = False
		self.name = self.info = self.range = self.effect = self.pinfo = self.other = ''
		self.inners = 0
		self.pid = []
		self.text = self.p.gettext(url)
	
	def handle_starttag(self, tag, attrs):
		attrs = dict(attrs)
		hid = attrs.get('id','')
		if self.in_range and attrs.get('colspan','') == '3':
			self.in_ranges = True
		if tag == 'h1' and hid == 'firstHeading':
			self.in_name = True
		elif tag == 'font':
			self.in_font = True
		elif tag == 'th':
			self.in_th = True
		elif tag == 'ul':
			self.in_list = True
		elif tag == 'p':
			self.in_p = True
		elif tag == 'span':
			self.in_span = True
			if attrs.get('class','') == 'type-box-8-inner':
				self.in_inner = True
		elif tag == 'style':
			self.in_style = True
		if re.search('(.+?)变更',hid):
			self.in_effect = False
		elif hid == '招式附加效果':
			self.in_effect = True
		elif hid == '可以学会该招式的宝可梦':
			self.in_effect = False
			self.in_learn = True
		# 获得宝可梦id：
		if self.in_learn:
			if tag == 'tbody':
				self.in_table = True
			elif self.in_table and tag == 'tr':
				self.in_tr = True
			elif self.in_tr and tag == 'td':
				self.in_td = True
				# 可以学习招式的宝可梦的文字描述，比如 001~807：
				if self.ofall and attrs.get('class','') == 'at-c':
					self.in_other = True
			# 下一行：
			elif self.in_other and tag == 'br':
				self.other += ' '
			# 其他形态：
			elif self.in_td and tag == 'small':
				self.in_form = True
			# 结束：
			elif self.in_table == False and (hid == '动画中' or hid == '名字' or hid == '细节' or hid == '漫画中' or hid == '招式动画'):
				self.pid = sorted(set(self.pid),key=self.pid.index)
				if self.pinfo != '' and self.pid != []:
					self.pinfo += '/'
				self.pid = self.pinfo + '/'.join(self.pid)
				self.in_learn = False

	def handle_data(self, data):
		data = str(data).strip(' \n')
		# 获得文字描述：
		if self.in_learn and self.in_p and self.in_th == False:
			self.pinfo += data
		# 获得招式名称：
		if self.in_name:
			if re.search('(.+?)（招式）',data):
				self.name = re.search('(.+?)（招式）',data).group(1)
			else:
				self.name = data
			self.in_name = False
		if data == '对战':
			self.in_battle = True
		# 获得招式战斗信息，比如 非接触、受守住影响：
		if self.in_list and self.in_battle:
			self.info += data
		# 获得招式影响范围：
		if data == '范围':
			self.in_battle = False
			self.in_range = True
		elif self.in_ranges:
			self.range = data
			self.in_range = False
			self.in_ranges = False
		# 获得招式附加效果：
		if data == '招式附加效果' and self.in_effect:
			self.in_effect = True
		elif self.in_effect and data != '' and (self.in_p or self.in_list):
			# 确保不会录入无关信息：
			if self.in_span == False and self.in_style == False:
				self.effect += data
				self.inners = 0
			elif self.in_inner:
				if self.inners > 0:
					self.effect += '/'
				self.effect += data
				self.inners += 1
		# 获得可以学习招式的宝可梦列表：
		if self.in_tr and data != '':
			if self.in_td:
				# 组合招式学习范围的信息：
				if self.in_other:
					self.other += data
				# 设置id：
				elif re.search('^[0-9]+$',data) and self.in_font == False:
					self.pid.append(data)
				# 其他形态：
				elif self.pid != [] and self.in_form:
					self.pid[-1] += ' '
					self.pid[-1] += data
					self.in_form = False
				# 可学习招式的宝可梦范围，比如 001~807：
				elif re.search('^([0-9]+?)～([0-9]+?)$',data):
					self.pid.append(data)
					self.ofall = True
		# 离开招式学习范围的读取，进入下一个项目：
		elif self.in_other:
			self.pid[-1] += ':'
			self.pid[-1] += self.other
			self.other = ''
			self.ofall = False
			self.in_other = False

	def handle_endtag(self, tag):
		if tag == 'tbody':
			self.in_table = False
		elif tag == 'font':
			self.in_font = False
		elif tag == 'th':
			self.in_th = False
		elif tag == 'tr':
			self.in_tr = False
		elif tag == 'td':
			self.in_td = False
		elif tag == 'li' and self.in_list and self.in_battle:
			self.info += '/'
		elif tag == 'ul':
			self.in_list = False
			if self.in_battle:
				self.info = self.info.strip('/')
				self.in_battle = False
		elif tag == 'p':
			self.in_p = False
		elif tag == 'small':
			self.in_form = False
		elif tag == 'span':
			self.in_span = False
			self.in_inner = False
		elif tag == 'style':
			self.in_style = False
			
def test():
	m = moveinfo(r'https://wiki.52poke.com/wiki/%E8%88%8D%E8%BA%AB%E5%86%B2%E6%92%9E%EF%BC%88%E6%8B%9B%E5%BC%8F%EF%BC%89')
	m.feed(m.text)
	move = [m.name, m.info, m.range, m.effect, m.pid]
	print(move)
	m = moveinfo(r'https://wiki.52poke.com/wiki/%E9%A3%9E%E5%BC%B9%E9%92%88%EF%BC%88%E6%8B%9B%E5%BC%8F%EF%BC%89')
	m.feed(m.text)
	move = [m.name, m.info, m.range, m.effect, m.pid]
	print(move)
	m = moveinfo(r'https://wiki.52poke.com/wiki/%E5%A6%96%E7%B2%BE%E4%B9%8B%E9%94%81%EF%BC%88%E6%8B%9B%E5%BC%8F%EF%BC%89')
	m.feed(m.text)
	move = [m.name, m.info, m.range, m.effect, m.pid]
	print(move)
	m = moveinfo(r'https://wiki.52poke.com/wiki/%E4%BC%8F%E7%89%B9%E6%94%BB%E5%87%BB%EF%BC%88%E6%8B%9B%E5%BC%8F%EF%BC%89')
	m.feed(m.text)
	move = [m.name, m.info, m.range, m.effect, m.pid]
	print(move)
	m = moveinfo(r'https://wiki.52poke.com/zh-hans/%E5%86%B0%E5%86%B7%E8%A7%86%E7%BA%BF%EF%BC%88%E6%8B%9B%E5%BC%8F%EF%BC%89')
	m.feed(m.text)
	move = [m.name, m.info, m.range, m.effect, m.pid]
	print(move)
	m.close()

if __name__ == "__main__":
	test()